package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplicationFile;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationfile.LindiSubsidyApplicationFileQueryVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationfile.LindiSubsidyApplicationFileVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationFileMapper;
import com.minimalist.lindi.service.LindiSubsidyApplicationFileService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 林地指标交易平台_补贴申请文件表 服务层实现。
 *
 * @author 11987
 * @since 2025-06-09
 */
@Service
public class LindiSubsidyApplicationFileServiceImpl extends ServiceImpl<
        LindiSubsidyApplicationFileMapper, LindiSubsidyApplicationFile>
        implements LindiSubsidyApplicationFileService{

    private static final Logger logger = LoggerFactory.getLogger(LindiSubsidyApplicationFileServiceImpl.class);

    @Autowired
    private LindiSubsidyApplicationFileMapper mapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubsidyApplicationFile(
            LindiSubsidyApplicationFileVO subsidyApplicationFileVO){
        LindiSubsidyApplicationFile record = BeanUtil.copyProperties(
                subsidyApplicationFileVO
                , LindiSubsidyApplicationFile.class);
        int insert = mapper.insert(record, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                "添加补贴申请文件失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationFileBySubsidyApplicationFileId(Long subsidyApplicationFileId){
        LindiSubsidyApplicationFile record = mapper.selectOneById(subsidyApplicationFileId);
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请文件不存在"));
        // 删除文件记录
        int delete = mapper.deleteById(subsidyApplicationFileId);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                "删除补贴申请文件失败"));
        // 删除实际文件
        try {
            R<Void> result = fileFeignClient.deleteFile(record.getFileId());
            if (result.getCode() != HttpStatus.HTTP_OK) {
                logger.warn("删除补贴申请文件失败，文件ID: {}, 错误码: {}", record.getFileId(), result.getCode());
            }
        } catch (Exception e) {
            logger.warn("删除补贴申请文件时发生异常，文件ID: {}, 异常信息: {}", record.getFileId(), e.getMessage(), e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubsidyApplicationFileBySubsidyApplicationFileId(
            LindiSubsidyApplicationFileVO subsidyApplicationFileVO){
        LindiSubsidyApplicationFile record = mapper
                .selectSubsidyApplicationFileByFileId(
                        subsidyApplicationFileVO.getFileId());
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请文件不存在"));
        BeanUtil.copyProperties(subsidyApplicationFileVO, record
                , CopyOptions.create().ignoreNullValue());
        int update = mapper.update(record);
        Assert.isTrue(update == 1, () -> new BusinessException(
                "修改补贴申请文件失败"));
    }

    @Override
    public PageResp<LindiSubsidyApplicationFileVO> getPageSubsidyApplicationFileList(
            LindiSubsidyApplicationFileQueryVO queryVO){
        // 分页查询
        Page<LindiSubsidyApplicationFile> recordPage = mapper
                .selectPageSubsidyApplicationFileList(queryVO);
        // 数据转换
        List<LindiSubsidyApplicationFileVO> voList = BeanUtil.copyToList(
                recordPage.getRecords()
                , LindiSubsidyApplicationFileVO.class);
        // 获取文件详细信息
        for (LindiSubsidyApplicationFileVO vo : voList){
            R<FileVO> fileResult = fileFeignClient.getFileByFileId(vo.getFileId());
            if (fileResult.getCode() == HttpStatus.HTTP_OK && fileResult.getData() != null){
                vo.setFile(fileResult.getData());
            }
        }
        return new PageResp<>(voList, recordPage.getTotalRow());
    }

    @Override
    public LindiSubsidyApplicationFileVO getSubsidyApplicationFileBySubsidyApplicationFileId(Long subsidyApplicationFileId){
        LindiSubsidyApplicationFile record = mapper.selectSubsidyApplicationFileByFileId(
                subsidyApplicationFileId);
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请文件不存在"));
        LindiSubsidyApplicationFileVO vo = BeanUtil.copyProperties(record
                , LindiSubsidyApplicationFileVO.class);
        // 获取文件详细信息
        R<FileVO> fileResult = fileFeignClient.getFileByFileId(record.getFileId());
        if (fileResult.getCode() == HttpStatus.HTTP_OK && fileResult.getData() != null){
            vo.setFile(fileResult.getData());
        }
        return vo;
    }

    @Override
    public PageResp<LindiSubsidyApplicationFileVO> getSubsidyApplicationFileListBySubsidyApplicationId(Long subsidyApplicationId){
        // 查询文件列表
        List<LindiSubsidyApplicationFile> recordList = mapper
                .selectFileListBySubsidyApplicationId(subsidyApplicationId);
        // 数据转换
        List<LindiSubsidyApplicationFileVO> voList = BeanUtil.copyToList(
                recordList, LindiSubsidyApplicationFileVO.class);
        // 获取文件详细信息
        for (LindiSubsidyApplicationFileVO vo : voList){
            R<FileVO> fileResult = fileFeignClient.getFileByFileId(vo.getFileId());
            if (fileResult.getCode() == 200 && fileResult.getData() != null){
                vo.setFile(fileResult.getData());
            }
        }
        return new PageResp<>(voList, voList.size());
    }
}